define(['./../../initial'],
    function() {
        var app = angular.module('App.home');
        app.controller('notice.infoCtrl', ['$timeout', '$scope', '$rootScope', '$state', '$stateParams', 'applierList', 'Project', 'showMsg',
            function($timeout, $scope, $rootScope, $state, $stateParams, applierList, Project, showMsg) {
                //获取工程列表
                Project.getAllCityList().then(function(list) {
                    $scope.cityList = list;
                });
                //赋值申请者
                $scope.applierList = applierList;
                //初始化下拉列表
                $timeout(function() {
                    $('.ui.dropdown').dropdown();
                }, 800);
                //是否显示权限项目列表
                $scope.showAuthorProject = null;
                $scope.curPerson = null;
                //重新给citylist的checked赋值
                var _refreshProjectChecked = function() {
                    $scope.cityList.forEach(function(city) {
                        city.lines.forEach(function(line) {
                            line.projects.forEach(function(prj) {
                                if ($scope.curPerson.authorizedProjectList.indexOf(prj.prjId) >= 0) {
                                    prj.checked = true;
                                } else {
                                    prj.checked = false;
                                }
                            });
                        });
                    });
                };

                // function _asignProejctForDepartAdministrator(person) {
                //     $scope.cityList.forEach(function(city) {
                //         city.lines.forEach(function(line) {
                //             line.projects.forEach(function(prj) {
                //                 if (prj.department === person.department) {
                //                     prj.checked = true;
                //                 } else {
                //                     prj.checked = false;
                //                 }
                //             });
                //         });
                //     });
                // }

                //设置用户的权限
                $scope.changeAuthen = function(person, value) {
                    person.authentication = value;
                    person.authorizedProjectList = person.authorizedProjectList || [];
                    //视觉处理
                    if (value === "项目经理" || value === "普通用户") {
                        if ($scope.curPerson) {
                            $scope.toggleAuthorProjectList(false);
                            $timeout(function() {
                                $scope.toggleAuthorProjectList(true);
                            }, 500);
                        } else {
                            $scope.toggleAuthorProjectList(true);
                        }
                    } else {
                        if ($scope.curPerson) {
                            $scope.toggleAuthorProjectList(false);
                        }
                    }
                    //赋值给当前激活的人员
                    $scope.curPerson = person;
                    _refreshProjectChecked();

                };

                //设置用户的权限项目
                $scope.toggleAuthorProjectList = function(toggle) {
                    if (typeof(toggle) === 'undefined') {
                        $scope.showAuthorProject = $scope.showAuthorProject ? false : true;
                    } else {
                        $scope.showAuthorProject = toggle;
                    }
                };
                //切换当前的工程勾选
                $scope.togglePrjCheck = function(prj) {
                    if (!$scope.curPerson) return;
                    prj.checked = !prj.checked;
                    if (prj.checked) {
                        $scope.curPerson.authorizedProjectList.push(prj.prjId);
                    } else {
                        var index = $scope.curPerson.authorizedProjectList.indexOf(prj.prjId);
                        $scope.curPerson.authorizedProjectList.splice(index, 1);
                    }
                };

                //批准或者拒绝一个用户
                $scope.handleApplier = function(person, accept) {
                    //有效性验证
                    if (accept === true) {
                        if (person.authentication.indexOf('权限') >= 0) {
                            showMsg('请指定' + person.realName + '的用户权限', 'info');
                            return;
                        } else if (person.authentication === "项目经理" || person.authentication === '普通用户') {
                            if (person.authorizedProjectList.length === 0) {
                                showMsg('请至少为' + person.realName + '分配一个项目', 'info');
                                return;
                            }
                        }
                    }

                    person.postHandleApplier(accept).then(function(result) {
                        if (result) {
                            person.finish = true;
                            $timeout(function() {
                                for (var i = 0; i < $scope.applierList.length; i++) {
                                    if ($scope.applierList[i].userName === person.userName) {
                                        $scope.applierList.splice(i, 1);
                                        break;
                                    }
                                }
                                if (accept) {
                                    showMsg('审核成功!', 'info');
                                } else {
                                    showMsg('已拒绝该用户的请求!', 'info');
                                }
                            }, 300);
                            if ($scope.curPerson !== null && ($scope.curPerson.authentication === "项目经理" || $scope.curPerson.authentication === "普通用户")) {
                                $scope.toggleAuthorProjectList(false);
                            }
                            $scope.curPerson = null;
                        } else {
                            showMsg('发生错误', 'alert');
                        }
                    });
                };

            }
        ]);
    });
